/**
 * yiche mail base class module
 */
const md5 = require("md5-node");
const fetch = require("node-fetch");
const FormData = require("form-data");

module.exports = class Mail {
  static async send(title, to, message) {
    const mailType = "1";
    const appId = "27619";
    const sender = "qianxun@yiche.com";
    const tagId = "1567397234334060544";
    const subject = title;
    const toList = to;
    const appKey = "b73b7827ab94e69e58e66d4b1ece847820b9c31d";

    const salt = `${appId}${mailType}${tagId}${sender}${toList}${subject}${appKey}`;
    const signature = md5(salt);

    let formdata = new FormData();
    formdata.append("mailType", mailType);
    formdata.append("appId", appId);
    formdata.append("sender", sender);
    formdata.append("tagId", tagId);
    formdata.append("subject", subject);
    formdata.append("toList", toList);
    formdata.append("signature", signature);
    formdata.append("body", message);

    await fetch(
      "http://mail-api.bitauto.com/mail-manager/mail/api/v1/sendMail/",
      {
        method: "POST",
        body: formdata,
      }
    );
  }
};
